/*
 * @Descripttion: 
 * @version: 
 * @Author: by
 * @Date: 2021-05-27 15:05:45
 * @LastEditors: by
 * @LastEditTime: 2021-10-11 10:48:47
 */

// 手机号检查
export const checkPhone = function (phone){ 
    let reg = /^[1][3,4,5,7,8,9][0-9]{9}$/
    if (!reg.test(phone)) {
      return false
    } else {
      return true
    }
}

// 日期时间格式化
export function formatTimestamp(time = 0, fmt = 'yyyy-MM-dd hh:mm:ss') {
  if(time === 0 || !time) {
    return ''
  }

  let date = new Date(time)
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  }
  let o = {
    'M+': date.getMonth() + 1,
    'd+': date.getDate(),
    'h+': date.getHours(),
    'm+': date.getMinutes(),
    's+': date.getSeconds()
  };
  for (let k in o) {
    if (new RegExp(`(${k})`).test(fmt)) {
      let str = o[k] + ''
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : ('00' + str).substr(str.length))
    }
  }
  return fmt
}

// 文件流下载
export function downloadFile(data,fileName, fileType = 'application/vnd.ms-excel,charset=utf-8') {  // 后台返回数据、文件名、文件类型(默认excel, word: application/msword)
  let blob = new Blob([data],{ type: fileType })
  let downloadElement = document.createElement('a')
  let href = window.URL.createObjectURL(blob) //创建下载的链接
  downloadElement.href = href
  downloadElement.download = fileName //下载后文件名
  document.body.appendChild(downloadElement)
  downloadElement.click() //点击下载
  document.body.removeChild(downloadElement) //下载完成移除元素
  window.URL.revokeObjectURL(href) //释放掉blob对象

}
